// 表单验证工具类

export const BaseRules = {
    pwd: [{
        required: true,
        message: '密码不能为空',
        trigger: 'blur'
    },{
        type: 'string',
        min: 6,
        message: '密码至少为6个字符',
        trigger: 'blur'
    }],
    mobile: [{
        message: '请填写11位手机号码',
        pattern: /^1\d{10}$/,
        trigger: 'blur'
    }],
    mobile_required: [{
        required: true,
        message: '请填写11位手机号码',
        pattern: /^1\d{10}$/,
        trigger: 'blur'
    }],
    email: [{
        message: '请填写电子邮箱',
        pattern: /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/ ,
        trigger: 'blur'
    }],
    email_required: [{
        required: true,
        message: '请填写电子邮箱',
        pattern: /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/ ,
        trigger: 'blur'
    }]
}

export const getRule = function(key) {
    return BaseRules[key]
}

export const getRequired = function (_cfg) {
    let cfg = Object.assign({
        type: 'string',
        label: ''
    }, _cfg)
    return {
        type: cfg.type,
        required: true,
        message: cfg.message || `${cfg.label}不能为空`,
        trigger: 'blur'
    }
}

export const getMin = function (_cfg) {
    let cfg = Object.assign({
        label: '',min: 6
    }, _cfg)
    return {
        type: 'string',
        min: cfg.min,
        message: `${cfg.label}至少为${cfg.min}个字符`,
        trigger: 'blur'
    }
}

export const getMax = function (_cfg) {
    let cfg = Object.assign({
        label: '',max: 20
    }, _cfg)
    return {
        type: 'string',
        max: cfg.max,
        message: `${cfg.label}最多为${cfg.max}个字符`,
        trigger: 'blur'
    }
}
